// pages/list3/list3.js

const app = getApp()
var landui = require("../../utils/util.js");



Page({

  /**
   * 页面的初始数据
   */
  data: {
    domain: app.globalData.domain,
    trueDomain: app.globalData.trueDomain,
    current_nav: 'toilet',
    list_column:[],
    info_top: [],
    list2: [],
    cid:1,
    url:'/pages/toilet/detail',
    msg:'上拉加载更多~',
    current_page:1,
    per_page:7,
    total_pages:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 栏目 http://jk.hnla.cn/api/articleTypes?classify=public_security
    var that = this;
    //栏目
    landui.common.post({
      module: 'articleTypes',
      param: { classify: 'public_security'},
      success: function (res) {
        that.setData({
          list_column: res.data,
        })
      },
      fail: function () { }
    });
     // 头条 http://jk.hnla.cn/api/topArticle?classify=3
    landui.common.post({
      module: 'topArticle',
      param: {classify:that.data.cid},
      success: function (res) {
        res.data[0].add_time = res.data[0].add_time.substring(0, 10)
        that.setData({
          info_top: res.data[0],
        })
      },
      fail: function () {}
    });
   
    // 列表 http://jk.hnla.cn/api/articles?page=2&number=4&classify=3
    landui.common.post({
      module: 'articles',
      param: { number: that.data.per_page, page: that.data.current_page, classify:that.data.cid},
      success: function (res) { 
        var i = 0;
        if(res.data){
          that.list2 = res.data;
          for (i; i < res.data.length; i++) {
            that.list2[i]['add_time'] = res.data[i].add_time.substring(0, 10)
          }
        }
        if(res.meta.pagination.total_pages == 1){
          that.msg ='已经到底啦~'
        }else{
          that.current_page = res.meta.pagination.current_page+1;
          that.total_pages = res.meta.pagination.total_pages
        }
        that.setData({
          list2:that.list2,
          msg:that.msg,
          total_pages: res.meta.pagination.total_pages
        })
      },
      fail: function () {
      }
    });
  },

  // 栏目切换
  btnSwitch: function (e) {
    var that = this;
    var cid = e.currentTarget.dataset.cid
    var msg = '上拉加载更多~'
    that.setData({
      cid: cid,
      current_page: 1,
      total_pages: 1
      
    })
    // 头条
    landui.common.post({
      module: 'topArticle',
      param: { classify: cid },
      success: function (res) {
        if(res.data.length>=1){
          res.data[0].add_time = res.data[0].add_time.substring(0, 10)
          that.setData({
            info_top: res.data[0],
          })
        }else{
          that.setData({
            info_top: [],
          })
        }  
      },
      fail: function () {
      }
    });
    // 列表
    landui.common.post({
      module: 'articles',
      param: { number: that.data.per_page, page: that.data.current_page, classify:cid },
      success: function (res) {
        var i = 0;
        if(res.data.length>=1){
          var newData = res.data;
          for (i; i < newData.length; i++) {
            newData[i]['add_time'] = res.data[i].add_time.substring(0, 10)
          }
        }else{
          newData = ''
        }
    
        if (res.data.length<2 && res.meta.pagination.total_pages == 1) {
        
           msg = '已经到底啦~'
        } else {
          that.current_page = res.meta.pagination.current_page + 1;
          that.total_pages = res.meta.pagination.total_pages
        }
        that.setData({
          list2: newData,
          total_pages: res.meta.pagination.total_pages,
          msg:msg
        })
      },
      fail: function () {}
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      var that = this;
    if (that.data.current_page+1 <= that.data.total_pages){
        landui.common.post({
          module: 'articles',
          param: { number: that.data.per_page, page: that.data.current_page+1, classify: that.data.cid },
          success: function (res) {
            var i = 0;
            if (res.data) {
              var newData = res.data;
              for (i; i < newData.length; i++) {
                newData[i]['add_time'] = res.data[i].add_time.substring(0, 10)
              }
            }
          
            that.setData({
              list2: that.data.list2.concat(newData),
              current_page: res.meta.pagination.current_page,
              total_pages: res.meta.pagination.total_pages
            })
          },
          fail: function () { }
        });
      }else{
        that.setData({
          msg:'已经到底啦~'
        })
      }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})